package com.xinzhi.service.impl;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinzhi.mapper.TAuditMapper;
import com.xinzhi.model.po.TAudit;
import com.xinzhi.service.IAuditZz;
import com.xinzhi.util.RedisUtils;
import com.xinzhi.util.Result;
import com.xinzhi.util.GetFileFront;
import com.xinzhi.util.MailUtil;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
@Primary
public class ZzAuditImpl implements IAuditZz {

    private int id1=1;
    private int year1;
    private int year2=2021;
    private String release_state1;
    private int pointid1;
    private int grade1;
    private String key1;
    private String key2;
    private String zzId=null;
    private String zzSKey=null;
    @Resource
    private TAuditMapper tAuditMapper;

    @Resource
    private MailUtil mailUtil;
    @Resource
    private RedisUtils redisUtils;

    @Override
    public Result getAll(int year, String release_state, int pointid, int grade,int page,int pagesize) {

//        生成

        String yearR=String.valueOf(year);
        String pointidR=String.valueOf(pointid);
        String gradeR=String.valueOf(grade);
        String page1=String.valueOf(page);
        String pagesize1=String.valueOf(pagesize);
        String keyB="zz"+yearR+release_state;
        String keyS=pointidR+gradeR+page1+pagesize1;
        //记录进入预审核发布等操作前页面状态
        key1=keyB;
        key2=keyS;

        Object hget = redisUtils.hget(keyB,keyS);
//        List<TAudit> audit = (List<TAudit>) hget;
        if (hget!=null){
            int total = (int)redisUtils.hget(keyB+"total",keyS);

            System.out.println("redis执行了");

            return Result.okMessage(total, JSONUtil.parseArray(hget));
        }
        QueryWrapper<TAudit> wrapper = new QueryWrapper<>();
        wrapper.eq("year", year).eq("release_state", release_state).eq("pointid", pointid).eq("grade", grade).eq("auditstate","待审核");
//        List<TAudit> main = tAuditMapper.selectList(wrapper);
//        分页

        Page<TAudit> pageList= new Page<>(page, pagesize);
        Page<TAudit> result = tAuditMapper.selectPage(pageList, wrapper);
        List<TAudit> main = result.getRecords();
        int total=(int)result.getTotal();
        if (main!=null){
            redisUtils.hset(keyB,keyS,main);
            redisUtils.hset(keyB+"total",keyS,total);
            JSONArray objects = JSONUtil.parseArray(main);
            year1=year;
            release_state1=release_state;
            pointid1=pointid;
            grade1=grade;
            return Result.okMessage(total,objects);
        }
        return Result.error(555,"失败");
    }



    @Override
    public Result getPreAudit(int id) {
        String s = String.valueOf(id);
        TAudit tAudit = tAuditMapper.selectById(id);

        String zzAuditId ="zz"+s;
        Object hget = redisUtils.hget(zzAuditId,tAudit.getUnit());
        if (hget!=null){
            System.out.println("redis执行了");
            return Result.ok(JSONUtil.parse(hget));

        }
        TAudit tAudit1 = tAuditMapper.selectById(id);
        if (tAudit1.getUnit()!=null){
            redisUtils.hset(zzAuditId,tAudit.getUnit(),tAudit);
            JSON parse = JSONUtil.parse(tAudit1);
            return Result.ok(parse);
        }
        return Result.error(555,"失败");
    }

    @Override
    public Result updatePreAudit(int id,String auditopinion, double auditnum) {
        if (key1!=null&&key2!=null){
            redisUtils.hdel(key1,key2);//删除redis集合数据
        }
        if (zzId!=null&&zzSKey!=null){
            redisUtils.hdel(zzId,zzSKey);//删除redis集合数据
        }

//        int i = specialMapper1.updatePreAudit(auditopinion,id1,auditnum,auditstate);
        UpdateWrapper<TAudit> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id).set("auditopinion",auditopinion).set("auditnum",auditnum).set("release_state","已保存");
        int i = tAuditMapper.update(null, updateWrapper);
        redisUtils.hdel(key1,key2);//删除redis集合数据

//        if (i>0){
//            QueryWrapper<TAudit> wrapper = new QueryWrapper<>();
//            wrapper.eq("year", year1);
//            wrapper.eq("release_state", release_state1);
//            wrapper.eq("pointid", pointid1);
//            wrapper.eq("grade", grade1);
//            List<TAudit> main = tAuditMapper.selectList(wrapper);
////            List<TSpecial> main = specialMapper1.getMain(year1, release_state1,pointid1,grade1);
            if (i>0){
//                JSONArray objects = JSONUtil.parseArray(main);
                return Result.ok("成功");
            }
//        }
        return Result.error(555,"失败");

    }

    @Override
    public Result updateReleaseState(int id, String release_state) {
        if (key1!=null&&key2!=null){

            redisUtils.hdel(key1,key2);//删除redis集合数据
        }
        if (zzId!=null&&zzSKey!=null){
            redisUtils.hdel(zzId,zzSKey);//删除redis集合数据
        }
        UpdateWrapper<TAudit> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id).set("release_state",release_state);
        int i = tAuditMapper.update(null, updateWrapper);
        redisUtils.hdel(key1,key2);//删除redis集合数据

//        int i = specialMapper1.updateRelease_state(id,release_state);
        int i1=0;
        System.out.println(id);
        System.out.println(release_state);
        //获取真实姓名
//        User user =(User)(SecurityContextHolder.getContext().getAuthentication().getPrincipal());
//        System.out.println(user.getName());
        //test
//        user.setName("张泽");
//        String people=user.getName();
        String people="张泽1";
//        System.out.println(user.getName());

        String nPeople=null;
        if (i>0){
            if ("已审核".equals(release_state)){
                //改台账评分汇总的审核经办人
                //根据id获取单位
                TAudit tSpecial = tAuditMapper.selectById(id);

                String unit = tSpecial.getUnit();
                int year = tSpecial.getYear();

//                //判断是否有这个单位
//                int panduan = 1;
//                List<TSummary> tSummaries = zzSummaryMapper.selectList(null);
//                for (TSummary tSummary:tSummaries){
//                    System.out.println(tSummary);
//                    if (tSummary.getUnit().equals(unit) && tSummary.getYear()==year){
//                        panduan=2;
//                    }
//                }
//                if (panduan==1){
//                    return Result.ok("Summary表中无该年份数据");
//                }
//
//                QueryWrapper<TSummary> wrappersu = new QueryWrapper<>();
//                wrappersu.eq("unit", unit).eq("year",year);
//                TSummary tSummary = zzSummaryMapper.selectOne(wrappersu);
//                String s = tSummary.getPeople();
//                System.out.println("名字为：");
//                System.out.println(s);
//                if (tSummary.getUnit()==null){
//                    return Result.ok("Summary表中无该年份数据");
//                }
////                String s = specialMapper1.peopleName(unit);
//                if(s==null){
//                    UpdateWrapper<TSummary> updateWrapper1 = new UpdateWrapper<>();
//                    updateWrapper1.eq("unit", unit).eq("year",year).set("people",people);
////                    specialMapper1.updatePeople(id,people);
//                    int update = zzSummaryMapper.update(null, updateWrapper1);
//                }else if (s.contains(",")){
//                    String[] parts = s.split(",");
//                    for (String a:parts){
//                        if (a.equals(people)){
//                            i1++;
//                        }
//                    }
//                    if (i1==0){
//                        nPeople=s+","+people;
//                        System.out.println(nPeople);
//                    }else {
//                        nPeople=s;
//                    }
//                    //已经加完人了
//                    i1=0;
//                    UpdateWrapper<TSummary> updateWrapper1 = new UpdateWrapper<>();
//                    updateWrapper1.eq("unit", unit).eq("year",year).set("people",nPeople);
//                    int update = zzSummaryMapper.update(null, updateWrapper1);
////                    specialMapper1.updatePeople(id,nPeople);
//                }else {
//                    if (people.equals(s)){
//                        nPeople=s;
//                    }else {
//                        nPeople=s+","+people;
//                    }
//
//                    UpdateWrapper<TSummary> updateWrapper1 = new UpdateWrapper<>();
//                    updateWrapper1.eq("unit", unit).eq("year",year).set("people",nPeople);
//                    int update = zzSummaryMapper.update(null, updateWrapper1);
//
//                }
//
//
//                //改t_summary表中特殊加扣分的分值
//                double specialAuditNbNum = 0;//特殊加分审核分值
////                double reduceNum;//特殊扣分审核分值
////                double addAppealNum;//特殊加分申诉分值
////                double reduceAppealNum;//特殊加分申诉分值
//
//                QueryWrapper<TAudit> wrappersp = new QueryWrapper<>();
//                wrappersp.eq("unit", unit).eq("year",year);
//                List<TAudit> tSpecials = tAuditMapper.selectList(wrappersp);
//
////                List<SpecialAudit03> summary = specialMapper1.getSummary(unit);
////                for (TAudit specialAudit03:tSpecials){
////                    //判断其有没申诉，申诉了这个分就不算了
////                    if (specialAudit03.getDeleteid()!=1){
////                        if (specialAudit03.getGrade()==1 && specialAudit03.getPointid()==0){
////                            specialAuditNbNum+=specialAudit03.getAuditnum();
////
////                        }
////                        if (specialAudit03.getGrade()==1 && specialAudit03.getPointid()==1){
////                            specialAuditNbNum-=specialAudit03.getAuditnum();
////
////                        }
////                        if (specialAudit03.getGrade()==1 && specialAudit03.getPointid()==2){
////                            specialAuditNbNum+=specialAudit03.getAuditnum();
////
////                        }
////                        if (specialAudit03.getGrade()==1 && specialAudit03.getPointid()==3){
////                            specialAuditNbNum-=specialAudit03.getAuditnum();
////                        }
////
////                    }
////
////
////                }
//
//                UpdateWrapper<TSummary> updateWrapper1 = new UpdateWrapper<>();
//                updateWrapper1.eq("unit", unit).eq("year",year).set("specialscore",specialAuditNbNum);
//                int i2 = zzSummaryMapper.update(null, updateWrapper1);
////                int i2 = specialMapper1.updateSummary(unit, specialAuditNbNum);
//                System.out.println(specialAuditNbNum);
//                System.out.println(i2);
//                //获取单位id
//                QueryWrapper<TUnit> wrapper = new QueryWrapper<>();
//                wrapper.eq("unit",unit);
//                TUnit tUnit = zzUnitMapper.selectOne(wrapper);
//                System.out.println("tunit:");
//                System.out.println(tUnit.getCreatedata());
//                //根据单位id查邮件
//                QueryWrapper<User> wrapperUser = new QueryWrapper<>();
//                wrapperUser.eq("unid",tUnit.getId());
//                List<User> users = zzUserMapper.selectList(wrapperUser);
//                String sub = "审核人"+people+":";
//                if (users.get(0).getEmail()==null){
//                    return Result.ok("没有邮箱");
//                }
//
//                mailUtil.sendSimpleMail(users.get(0).getEmail(),sub,"您的申请已审核，请移步查看。");
                return Result.ok("成功审核");
            }else if ("已保存".equals(release_state)){
                return Result.ok("成功撤回");

            }


        }
        return Result.error(555,"失败");
    }

    @Override
    public Result getUpdateDate(String path) {

        String auditopinion=null;//审核意见
        double auditnum=0;//审核分值
        String a1="审核意见：";
        String a2="审核分值：";

        GetFileFront getFileFront = new GetFileFront();
        String file = getFileFront.getFile(path);
        String input = file;
        String[] lines = input.split("\n");

        for (String line : lines) {

            if (line.contains(a1)){
                int index = input.indexOf(line) + a1.length();
                auditopinion = input.substring(index);
                String[] lines1 = auditopinion.split("\r");
                auditopinion=lines1[0];
                System.out.println("aubit:");
                System.out.println(auditopinion);

            }
            if (line.contains(a2)){
                int index = input.indexOf(line) + a2.length();
                String output = input.substring(index);
                System.out.println("output:");
                System.out.println(output);
                auditnum = Double.parseDouble(output);


            }


        }
        TAudit tSpecial = tAuditMapper.selectById(id1);
        tSpecial.setAuditopinion(auditopinion);
        tSpecial.setAuditnum(auditnum);
        if (tSpecial.getUnit()!=null){
            return Result.ok(tSpecial);
        }

        return Result.error(555,"失败");
    }
}
